<?php


namespace App\Http\Controllers\Admin;


use App\DTOS\Admin\UserMemberDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\Equip;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;

class EquipIntensifyController extends Controller
{
    private Request $request;

    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;

    /**
     * EquipIntensifyController constructor.
     * @param Request $request
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(Request $request, UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->request = $request;
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
    }

    public function paginate(){
        $searchData = $this->request->all();
        $account = data_get($searchData,'account');
        $equipName = data_get($searchData,'equipName');
        global $gameArea;
        if ($account){
            $userMemberDTO = new UserMemberDTO(['username'=>$account]);
            $userMemberDTO->setFields(['id']);
            $paginate = $this->userMemberService->paginate($userMemberDTO);
            $memIds = collect($paginate->items())->pluck('id')->toArray();
            if (empty($memIds)){
                return ResponseUtil::error("没有数据");
            }
            $searchData['memIds'] = $memIds;
        }
        if ($equipName){
            $equipIds = Equip::query()->where('name','like',"%$equipName%")->pluck('id')->toArray();
            if (empty($equipIds)){
                return ResponseUtil::error("没有数据");
            }
            $searchData['equipIds'] = $equipIds;
        }
        $result = callRpc("/EquipIntensifyService/paginate",$searchData,['gameArea'=>$gameArea]);
        $memIds = collect($result['data'])->pluck('memId')->toArray();
        $members = $this->userMemberService->getByIds($memIds);
        foreach($result['data'] as &$datum){
            $member = collect($members)->where('id',$datum['memId'])->first();
            $member = array_only($member,['id','username']);
            $datum['member'] = $member;
            $datum['id'] =  (string)$datum['id'];
            $datum['uid'] =  (string)$datum['uid'];
            $attr = data_get($datum,'attr');
            $datum['attr'] = $attr ? json_decode($attr,true) : null;
        }
        return ResponseUtil::json($result);
    }
}
